package com.ruoyi.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.business.domain.BuyerInfo;
import com.ruoyi.business.mapper.BuyerInfoMapper;
import com.ruoyi.business.service.BuyerInfoService;
import com.ruoyi.business.utils.BadRequestException;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@RequiredArgsConstructor
public class BuyerInfoServiceImpl implements BuyerInfoService {

    private final BuyerInfoMapper buyerInfoMapper;
    private final SysUserMapper sysUserMapper;


    /**
     * 查询客户信息列表
     *
     * @param buyerInfo 客户信息
     * @return 客户信息
     */
    @Override
    public List<BuyerInfo> selectBuyerInfoList(BuyerInfo buyerInfo) {
        return buyerInfoMapper.selectBuyerInfoList(buyerInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertOrUpdateByWid(List<BuyerInfo> buyerInfos) {
        SysUser sysUser = sysUserMapper.selectUserByUserName(SecurityUtils.getUsername());
        Date now = DateUtils.getNowDate();
        for (BuyerInfo item : buyerInfos) {
            item.setCreateBy(sysUser.getNickName());
            item.setCreateTime(now);
            item.setUpdateBy(sysUser.getNickName());
            item.setUpdateTime(now);
        }
        buyerInfoMapper.insertOrUpdateBatch(buyerInfos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertOrUpdateByImport(List<BuyerInfo> buyerInfos) {
        SysUser sysUser = sysUserMapper.selectUserByUserName(SecurityUtils.getUsername());
        Date now = DateUtils.getNowDate();
        for (BuyerInfo item : buyerInfos) {
            if (item.getWid() == null) {
                throw new BadRequestException("客户编号不能为空！");
            }
            if (item.getCreateTime() == null) {
                item.setCreateTime(now);
            }
            if (StrUtil.isBlank(item.getCreateBy())) {
                item.setCreateBy(sysUser.getNickName());
            }
            item.setUpdateTime(now);
            item.setUpdateBy(sysUser.getNickName());
            }

            int batchSize = 1000;
            for (int i = 0; i < buyerInfos.size(); i += batchSize) {
                int end = Math.min(i + batchSize, buyerInfos.size());
                List<BuyerInfo> batchList = buyerInfos.subList(i, end);
                buyerInfoMapper.insertOrUpdateBatch(batchList);
            }
        }


    /**
     * 查询客户信息
     *
     * @param wid 客户信息主键
     * @return 客户信息
     */
    @Override
    public BuyerInfo selectBuyerInfoByWid(Long wid) {
        return buyerInfoMapper.selectBuyerInfoByWid(wid);
    }

    /**
     * 修改客户信息
     *
     * @param buyerInfo 客户信息
     * @return 结果
     */
    @Override
    public int updateBuyerInfo(BuyerInfo buyerInfo) {
        if (buyerInfo.getWid() == null) {
            throw new BadRequestException("客户编号不能为空！");
        }
        SysUser sysUser = sysUserMapper.selectUserByUserName(SecurityUtils.getUsername());
        buyerInfo.setUpdateTime(DateUtils.getNowDate());
        buyerInfo.setUpdateBy(sysUser.getNickName());
        return buyerInfoMapper.updateBuyerInfo(buyerInfo);
    }


    /**
     * 批量删除客户信息
     *
     * @param wids 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteBuyerInfoByWids(Long[] wids) {
        return buyerInfoMapper.deleteBuyerInfoByWids(wids);
    }

    @Override
    public int deleteBuyerInfoByWid(Long wid) {
        return buyerInfoMapper.deleteBuyerInfoByWid(wid);
    }
}
